package be.vdabantwerpen.gow.servlets;

import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import be.vdabantwerpen.gow.entities.GOWUser;
import be.vdabantwerpen.gow.services.UserService;
import be.vdabantwerpen.gow.services.UserServiceImpl;

/**
 * Servlet implementation class ProfielWijzigenServlet
 */
public class ProfielWijzigenServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;
	private static final String VIEW = "/WEB-INF/JSP/user/profielwijzigen.jsp";
	private final UserService userService = new UserServiceImpl();
	private static final String REDIRECT = "/user/profielgewijzigd";

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.getRequestDispatcher(VIEW).forward(request, response);
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		GOWUser user = null;
		GOWUser newUser = null;
		HttpSession session = request.getSession();
		// List<String> fouten = new ArrayList<String>();
		if (session != null) {
			user = (GOWUser) session.getAttribute("user");
			if (user != null) {
				String firstName = request.getParameter("firstName");
				if (firstName == null) {
					firstName = user.getFirstName();
				}
				String lastName = request.getParameter("lastName");
				if (lastName == null) {
					lastName = user.getLastName();
				}

				String street = request.getParameter("street");
				if (street == null) {
					street = user.getStreet();
				}

				int number = 0;
				String nummerAlsString = request.getParameter("number");
				if (nummerAlsString == null) {
					number = user.getNumber();
				} else {
					number = Integer.parseInt(nummerAlsString);
				}

				int postalCode = 0;
				String postcodeAlsString = request.getParameter("postalCode");
				if (postcodeAlsString == null) {
					postalCode = user.getPostalCode();
				} else {
					postalCode = Integer.parseInt(postcodeAlsString);
				}

				String town = request.getParameter("town");
				if (town == null) {
					town = user.getTown();
				}

				String userName = request.getParameter("userName");
				if (userName == null) {
					userName = user.getUserName();
				}

				String password = request.getParameter("password");
				if (password == null) {
					password = user.getPassword();
				}

				newUser = new GOWUser(firstName, lastName, street, number,
						postalCode, town, userName, password);
				userService.update(newUser);
			}
			session.setAttribute("user", newUser);
		}

		response.sendRedirect(request.getContextPath() + REDIRECT + "?userid="
				+ user.getUserId());
	}
}